package com.zytech.zspider.eventbus.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author siuming
 */
final class RabbitMQChannels {

    /**
     * @param connectionFactory
     * @param qname
     * @return
     * @throws IOException
     */
    static Channel ofQueueChannel(ConnectionFactory connectionFactory, String qname) throws IOException {
        Connection conn = connectionFactory.newConnection();
        Channel channel = conn.createChannel();
        channel.queueDeclare(qname, true, false, false, null);
        return channel;
    }

    /**
     * @param connectionFactory
     * @param tname
     * @return
     * @throws IOException
     */
    static Channel ofTopicChannel(ConnectionFactory connectionFactory, String tname) throws IOException {
        Connection conn = connectionFactory.newConnection();
        Channel channel = conn.createChannel();
        channel.exchangeDeclare(tname, "fanout");
        return channel;
    }

    /**
     * @param channel
     * @param tname
     * @return
     * @throws IOException
     */
    static String ofTopicChannelQueue(Channel channel, String tname) throws IOException {
        String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue, tname, "");
        return queue;
    }
}
